package com.zimi.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zimi.bean.Constants;
import com.zimi.mapper.*;
import com.zimi.model.*;
import com.zimi.util.ShiroUtils;
import com.zimi.util.UserUtil;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PrivilegeFunctionService {
    @Autowired
    private SysRolePrivilegeMapper sysRolePrivilegeMapper;

    @Autowired
    private SysPostPrivilegeMapper sysPostPrivilegeMapper;

    @Autowired
    private SysFunctionMapper sysFunctionMapper;

    @Autowired
    private SysRoleUserMapper sysRoleUserMapper;

    @Autowired
    private SysPostUserMapper sysPostUserMapper;

    @Autowired
    private SysDutyPrivilegeMapper sysDutyPrivilegeMapper;

    @Autowired
    private SysOrgPrivilegeMapper sysOrgPrivilegeMapper;

    @Autowired
    private SysOrgCateMapper sysOrgCateMapper;

    @Autowired
    private SysOrgCatePrivilegeMapper sysOrgCatePrivilegeMapper;

    /*
     * @author: wj
     * @description: 查询当前登录人得到授权的功能
     * @date: 2019/4/24 10:02
     * @param
     * @return
     */
    public List functionTreeList(List<Integer> posts) {
        // 1)取出用户信息
        SysUser sysUser = UserUtil.getUser();

        List<Integer> functionIdList = new ArrayList<>();

        // 2)指定岗位编号时
        if (posts != null && posts.size() == 2 && posts.get(0) > 0) {
            SysPostUser sysPostUser = sysPostUserMapper.selectOne(new QueryWrapper<SysPostUser>().lambda().eq(SysPostUser::getState, 1).eq(SysPostUser::getOrgId, posts.get(0)).eq(SysPostUser::getPostId, posts.get(1)).eq(SysPostUser::getUserId, sysUser.getId()));

            if (sysPostUser != null) {
                // 2-1)保存所选的岗位用户信息
                Session session = ShiroUtils.getSession();
                session.setAttribute(Constants.CURENT_POST_USER, sysPostUser);

                // 2-2)取得岗位权限
                List<SysPostPrivilege> sysPostPrivileges = sysPostPrivilegeMapper.selectList(new QueryWrapper<SysPostPrivilege>().lambda().eq(SysPostPrivilege::getPrivilegeState, 1).eq(SysPostPrivilege::getOrgId, posts.get(0)).eq(SysPostPrivilege::getPostId, posts.get(1)));

                if (sysPostPrivileges != null && sysPostPrivileges.size() > 0) {
                    for (SysPostPrivilege sysPostPrivilege : sysPostPrivileges) {
                        functionIdList.add(sysPostPrivilege.getFunctionId());
                    }
                }

                // 2-3)取得职务权限
                if (sysPostUser.getPostId() > 0) {
                    List<Integer> sysDutyPrivileges = sysDutyPrivilegeMapper.selectDutyPrivilegeList(sysPostUser.getPostId());

                    if (sysDutyPrivileges != null && sysDutyPrivileges.size() > 0) {
                        for (Integer tmpId : sysDutyPrivileges) {
                            functionIdList.add(tmpId);
                        }
                    }
                }

                // 2-4)取得机构权限和机构分类权限
                List<Integer> sysOrgPrivileges = sysOrgPrivilegeMapper.selectPrivilegeListByOrgId(sysPostUser.getOrgId());
                if (sysOrgPrivileges != null && sysOrgPrivileges.size() > 0) {
                    for (Integer tmpId : sysOrgPrivileges) {
                        functionIdList.add(tmpId);
                    }
                }
            }
        }

        // 3)取得角色菜单
        List<SysRoleUser> sysRoleUsers = sysRoleUserMapper.selectList(new QueryWrapper<SysRoleUser>().lambda().eq(SysRoleUser::getUserId, sysUser.getId()).eq(SysRoleUser::getState, 1));
        if (sysRoleUsers != null && sysRoleUsers.size() > 0) {
            List<Integer> idList = new ArrayList<>();
            for (SysRoleUser sru : sysRoleUsers) {
                idList.add(sru.getRoleId());
            }
            List<SysRolePrivilege> rolePrivileges = sysRolePrivilegeMapper.selectList(new QueryWrapper<SysRolePrivilege>().lambda().eq(SysRolePrivilege::getPrivilegeState, 1).in(SysRolePrivilege::getRoleId, idList));
            idList.clear();

            if (rolePrivileges != null && rolePrivileges.size() > 0) {
                for (SysRolePrivilege sysRolePrivilege : rolePrivileges) {
                    functionIdList.add(sysRolePrivilege.getFunctionId());
                }
            }
        }

        // 5)取得功能一览
        List<PrivilegeFunction> result = new ArrayList<>();
        PrivilegeFunction tmpPrivilegeFunction = null;

        if (functionIdList.size() > 0) {
            List<SysFunction> sysFunctionList = sysFunctionMapper.selectList(new QueryWrapper<SysFunction>().lambda().in(SysFunction::getId, functionIdList).eq(SysFunction::getFunctionState, 1).orderByAsc(SysFunction::getFunctionSort));

            if (sysFunctionList != null && sysFunctionList.size() > 0) {
                for (SysFunction sysFunction : sysFunctionList) {
                    tmpPrivilegeFunction = new PrivilegeFunction();
                    BeanUtil.copyProperties(sysFunction, tmpPrivilegeFunction);

                    result.add(tmpPrivilegeFunction);
                }
            }
        }

        return result;
    }

    /*
     * 返回指定用户的权限一览
     *   @
     * */
    public List<SysFunctionExtend> functionList(int userId) {
        List<SysFunctionExtend> result = new ArrayList<>();
        Map<Integer, SysFunctionExtend> sysFunctionExtendMap = new HashMap<>();
        SysFunctionExtend sysFunctionExtend = null;

        // 1)定义系统功能Map的扩展类型Map
        Map<Integer, SysFunctionExtend> functionExtendMap = new HashMap<>();
        List<Integer> ids = new ArrayList<>();
        List<SysRolePrivilege> sysRolePrivileges = null;
        List<Integer> functionIds = new ArrayList<>();

        // 2)用户拥有的角色权限
        List<SysRoleUser> sysRoleUsers = sysRoleUserMapper.selectList(new LambdaQueryWrapper<SysRoleUser>().eq(SysRoleUser::getState, 1).eq(SysRoleUser::getUserId, userId));
        if (sysRoleUsers != null && sysRoleUsers.size() > 0) {
            for (SysRoleUser sru : sysRoleUsers) {
                ids.add(sru.getRoleId());
            }

            sysRolePrivileges = sysRolePrivilegeMapper.selectList(new LambdaQueryWrapper<SysRolePrivilege>().eq(SysRolePrivilege::getPrivilegeState, 1).in(SysRolePrivilege::getRoleId, ids));
            if (sysRolePrivileges != null && sysRolePrivileges.size() > 0) {
                for (SysRolePrivilege srp : sysRolePrivileges) {
                    functionIds.add(srp.getFunctionId());
                }
            }
        }

        // 3)用户拥有的岗位权限
        List<SysPostPrivilege> sysPostPrivileges = sysPostPrivilegeMapper.selectPrivilegeByUser(userId);

        if (sysPostPrivileges != null && sysPostPrivileges.size() > 0) {
            for (SysPostPrivilege spp : sysPostPrivileges) {
                functionIds.add(spp.getFunctionId());
            }
        }

        // 4)用户拥有的岗位的单位和机构分类权限
        List<SysOrgPrivilege> sysOrgPrivileges = null;
        List<SysOrgCatePrivilege> sysOrgCatePrivileges = null;
        List<SysDutyPrivilege> sysDutyPrivileges = null;

        List<SysPostUser> sysPostUsers = sysPostUserMapper.selectList(new LambdaQueryWrapper<SysPostUser>().eq(SysPostUser::getState, 1).eq(SysPostUser::getUserId, userId));
        if (sysPostUsers != null && sysPostUsers.size() > 0) {
            ids.clear();

            for (SysPostUser spu : sysPostUsers) {
                ids.add(spu.getOrgId());
            }

            // 5)机构权限一览
            sysOrgPrivileges = sysOrgPrivilegeMapper.selectList(new LambdaQueryWrapper<SysOrgPrivilege>().eq(SysOrgPrivilege::getPrivilegeState, 1).in(SysOrgPrivilege::getOrgId, ids));
            if (sysOrgPrivileges != null && sysOrgPrivileges.size() > 0) {
                for (SysOrgPrivilege sop : sysOrgPrivileges) {
                    functionIds.add(sop.getFunctionId());
                }
            }

            // 6)机构分类一览
            List<SysOrgCate> sysOrgCates = sysOrgCateMapper.selectList(new LambdaQueryWrapper<SysOrgCate>().eq(SysOrgCate::getSocState, 1).in(SysOrgCate::getId, ids));

            if (sysOrgCates != null && sysOrgCates.size() > 0) {
                sysOrgCatePrivileges = sysOrgCatePrivilegeMapper.selectList(new LambdaQueryWrapper<SysOrgCatePrivilege>().eq(SysOrgCatePrivilege::getPrivilegeState, 1).in(SysOrgCatePrivilege::getOrgCateId, ids));
                if (sysOrgCatePrivileges != null && sysOrgCatePrivileges.size() > 0) {
                    for (SysOrgCatePrivilege socp : sysOrgCatePrivileges) {
                        functionIds.add(socp.getFunctionId());
                    }
                }
            }

            // 7)通过职务一览取得职务权限一览
            ids.clear();
            for (SysPostUser spu : sysPostUsers) {
                if (spu.getPostId() > 0) {
                    ids.add(spu.getPostId());
                }

            }

            if (ids.size() > 0) {
                sysDutyPrivileges = sysDutyPrivilegeMapper.selectList(new LambdaQueryWrapper<SysDutyPrivilege>().eq(SysDutyPrivilege::getPrivilegeState, 1).in(SysDutyPrivilege::getDutyId, ids));

                if (sysDutyPrivileges != null && sysDutyPrivileges.size() > 0) {
                    for (SysDutyPrivilege sdp : sysDutyPrivileges) {
                        functionIds.add(sdp.getFunctionId());
                    }
                }
            }
        }

        // 8)取出所有权限
        if (functionIds.size() > 0) {
            List<SysFunction> sysFunctions = sysFunctionMapper.selectList(new LambdaQueryWrapper<SysFunction>().eq(SysFunction::getFunctionState, 1).in(SysFunction::getId, functionIds).orderByAsc(SysFunction::getParentId, SysFunction::getFunctionSort, SysFunction::getId));

            if (sysFunctions != null && sysFunctions.size() > 0) {
                for (SysFunction sf : sysFunctions) {
                    sysFunctionExtend = new SysFunctionExtend();

                    sysFunctionExtend.setId(sf.getId());
                    sysFunctionExtend.setFunctionName(sf.getFunctionName());
                    sysFunctionExtend.setParentId(sf.getParentId());
                    sysFunctionExtend.setFunctionUrl(sf.getFunctionUrl());
                    sysFunctionExtend.setFunctionIcon(sf.getFunctionIcon());
                    sysFunctionExtend.setIconId(sf.getIconId());
                    sysFunctionExtend.setFunctionState(sf.getFunctionState());
                    sysFunctionExtend.setFunctionSort(sf.getFunctionSort());
                    sysFunctionExtend.setCreateUser(sf.getCreateUser());
                    sysFunctionExtend.setCreateTime(sf.getCreateTime());
                    sysFunctionExtend.setUpdateUser(sf.getUpdateUser());
                    sysFunctionExtend.setUpdateTime(sf.getUpdateTime());

                    result.add(sysFunctionExtend);
                    sysFunctionExtendMap.put(sysFunctionExtend.getId(), sysFunctionExtend);
                }
            }
        }

        // 9)用户拥有的角色权限
        if (sysRolePrivileges != null && sysRolePrivileges.size() > 0) {
            for (SysRolePrivilege srp : sysRolePrivileges) {
                sysFunctionExtend = (SysFunctionExtend) sysFunctionExtendMap.get(srp.getFunctionId());

                if (sysFunctionExtend != null) {
                    sysFunctionExtend.getRoleList().add(srp.getRoleId());
                }
            }
        }

        // 10)用户拥有的岗位权限
        if (sysPostPrivileges != null && sysPostPrivileges.size() > 0) {
            SysOrgPostPair sysOrgPostPair = null;
            for (SysPostPrivilege spp : sysPostPrivileges) {
                sysFunctionExtend = (SysFunctionExtend) sysFunctionExtendMap.get(spp.getFunctionId());

                if (sysFunctionExtend != null) {
                    sysOrgPostPair = new SysOrgPostPair();
                    sysOrgPostPair.setOrgId(spp.getOrgId());
                    sysOrgPostPair.setPostId(spp.getPostId());
                    sysFunctionExtend.getPostList().add(sysOrgPostPair);
                }
            }
        }

        // 11)用户拥有的机构权限
        if (sysOrgPrivileges != null && sysOrgPrivileges.size() > 0) {
            for (SysOrgPrivilege sop : sysOrgPrivileges) {
                sysFunctionExtend = (SysFunctionExtend) sysFunctionExtendMap.get(sop.getFunctionId());

                if (sysFunctionExtend != null) {
                    sysFunctionExtend.getOrgList().add(sop.getOrgId());
                }
            }
        }

        // 12)用户拥有的机构分类权限
        if (sysOrgCatePrivileges != null && sysOrgCatePrivileges.size() > 0) {
            for (SysOrgCatePrivilege socp : sysOrgCatePrivileges) {
                sysFunctionExtend = (SysFunctionExtend) sysFunctionExtendMap.get(socp.getFunctionId());

                if (sysFunctionExtend != null) {
                    sysFunctionExtend.getOrgCateList().add(socp.getOrgCateId());
                }
            }
        }

        // 13)用户拥有的职务权限一览
        if (sysDutyPrivileges != null && sysDutyPrivileges.size() > 0) {
            for (SysDutyPrivilege sdp : sysDutyPrivileges) {
                sysFunctionExtend = (SysFunctionExtend) sysFunctionExtendMap.get(sdp.getFunctionId());

                if (sysFunctionExtend != null) {
                    sysFunctionExtend.getDutyList().add(sdp.getDutyId());
                }
            }
        }

        return result;
    }
}
